﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Xml.Linq;

namespace DO.Workflows
{
    public class EmailInstruction : InstructionBase
    {
        #region props
        public bool UseOutlook { get; set; }
        public string SmtpServer { get; set; }
        public string FromAddress { get; set; }
        public string ToAddress { get; set; }
        public string CC { get; set; }
        public string Subject { get; set; }
        public bool RequireAuthentication { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public bool EnableSSL { get; set; }
        public int SmtpPort { get; set; }
        public List<string> Attachments { get; set; }
        public string Body { get; set; }
        public MailPriority Priority { get; set; }
        public Dictionary<string,string> BodyPlaceHolders { get; set; }
        #endregion

        #region ctor
        public EmailInstruction()
            : base(Guid.Empty, Guid.Empty)
        {
            this.UseOutlook = false;
            this.SmtpServer = string.Empty;
            this.FromAddress = string.Empty;
            this.ToAddress = string.Empty;
            this.CC = string.Empty;
            this.Subject = string.Empty;
            this.RequireAuthentication = false;
            this.UserName = string.Empty;
            this.Password = string.Empty;
            this.EnableSSL = false;
            this.SmtpPort = 25;
            this.Attachments = new List<string>();
            this.Body = string.Empty;
            this.Priority = MailPriority.Normal;
            this.BodyPlaceHolders=new Dictionary<string, string>();
        }

        public EmailInstruction(Guid componentID, Guid ticketID,
            bool useOutlook, string smtpServer,
            string fromAddr, string toAddr, string cc,
            string subj, bool auth,
            string userName, string password,
            bool enableSSL, int smtpPortNum,
            List<string> attachments,
            string body, MailPriority priority,
            Dictionary<string,string> placeHolders)
            : base(componentID, ticketID)
        {
            this.UseOutlook = useOutlook;
            this.SmtpServer = smtpServer;
            this.FromAddress = fromAddr;
            this.ToAddress = toAddr;
            this.CC = cc;
            this.Subject = subj;
            this.RequireAuthentication = auth;
            this.UserName = userName;
            this.Password = password;
            this.EnableSSL = enableSSL;
            this.SmtpPort = smtpPortNum;
            this.Attachments = attachments;
            this.Body = body;
            this.Priority = priority;
            this.BodyPlaceHolders = placeHolders;
        }
        #endregion

        #region Overrides of InstructionBase

        public override XElement Serialize()
        {
            XElement xe = base.Serialize();
            xe.Add(new XAttribute("UseOutlook", this.UseOutlook.ToString()));
            xe.Add(new XAttribute("FromAddress", this.FromAddress));
            xe.Add(new XAttribute("ToAddress", this.ToAddress));
            xe.Add(new XAttribute("CC",this.CC));
            xe.Add(new XAttribute("Subject", this.Subject));
            xe.Add(new XAttribute("RequireAuthentication", this.RequireAuthentication.ToString()));
            xe.Add(new XAttribute("UserName", this.UserName));
            xe.Add(new XAttribute("Password", this.Password));
            xe.Add(new XAttribute("EnableSSL", this.EnableSSL.ToString()));
            xe.Add(new XAttribute("SmtpPort",this.SmtpPort.ToString()));
            xe.Add(new XAttribute("Body", this.Body));
            xe.Add(new XAttribute("Priority",this.Priority.ToString()));
            if (this.Attachments != null && this.Attachments.Count > 0)
            {
                XElement attachments = new XElement("Attachments");
                xe.Add(attachments);
                foreach (string filePath in this.Attachments)
                {
                    XElement attachment = new XElement("Attachment",
                        new XAttribute("FilePath", filePath));
                    attachments.Add(attachment);
                }
            }
            if(this.BodyPlaceHolders !=null && this.BodyPlaceHolders.Count>0)
            {
                XElement placeHolders=new XElement("PlaceHolders");
                xe.Add(placeHolders);
                foreach(string key in this.BodyPlaceHolders.Keys)
                {
                    XElement placeholder=new XElement("PlaceHolder",
                        new XAttribute("Name",key),
                        new XAttribute("Value",this.BodyPlaceHolders[key]));
                    placeHolders.Add(placeholder);
                }
            }
            return xe;
        }

        public override InstructionBase Instantiate(XElement xe)
        {
            EmailInstruction instruction = (EmailInstruction)base.Instantiate(xe);
            instruction.UseOutlook = bool.Parse(xe.Attribute("UseOutlook").Value);
            instruction.FromAddress = xe.Attribute("FromAddress").Value;
            instruction.ToAddress = xe.Attribute("ToAddress").Value;
            instruction.CC = xe.Attribute("CC").Value;
            instruction.Subject = xe.Attribute("Subject").Value;
            instruction.RequireAuthentication = bool.Parse(xe.Attribute("RequireAuthentication").Value);
            instruction.UserName = xe.Attribute("UserName").Value;
            instruction.Password = xe.Attribute("Password").Value;
            instruction.EnableSSL = bool.Parse(xe.Attribute("EnableSSL").Value);
            instruction.SmtpPort = int.Parse(xe.Attribute("SmtpPort").Value);
            instruction.Body = xe.Attribute("Body").Value;
            instruction.Priority = (MailPriority) Enum.Parse(typeof (MailPriority), xe.Attribute("Priority").Value);
            foreach (XElement attachment in xe.Elements("Attachments").Elements("Attachment"))
            {
                instruction.Attachments.Add(attachment.Attribute("FilePath").Value);
            }
            instruction.BodyPlaceHolders=new Dictionary<string, string>();
            foreach(XElement placeHolder in xe.Elements("PlaceHolders").Elements("PlaceHolder"))
            {
                instruction.BodyPlaceHolders.Add(placeHolder.Attribute("Name").Value, placeHolder.Attribute("Value").Value);
            }
            return instruction;
        }

        #endregion
    }
}
